package com.newway.common.login;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.newway.common.context.UserInfo;
import com.newway.common.context.UserInfoContext;
import com.newway.common.exception.BusinessException;
import com.newway.common.mapper.UserInfoMapper;
import com.newway.common.result.Result;
import com.newway.common.utils.HttpUtil;
import com.newway.common.utils.JwtTokenUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@RestController
public class LoginController {

    @Resource
    JwtTokenUtil jwtTokenUtil;

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Resource
    UserInfoMapper userInfoMapper;

    @PostMapping("/login")
    public Result login(@RequestBody LoginBody loginBody){
        QueryWrapper<UserInfo> userInfoQueryWrapper = new QueryWrapper<>();
        UserInfo userInfo = null;
        if(StringUtils.isBlank(loginBody.getUserName())){
            return Result.fail("登录请求参数错误");
        }
        if("system".equals(loginBody.getLoginType())){
            userInfoQueryWrapper.eq("user_from","system");
        }
        userInfoQueryWrapper.eq("user_name",loginBody.getUserName());
        userInfoQueryWrapper.eq("password",loginBody.getPassword());
        userInfo = userInfoMapper.selectOne(userInfoQueryWrapper);
        if(userInfo == null){
            throw new BusinessException("当前用户信息不存在");
        }
        String uuid = UUID.randomUUID().toString().replace("-","");
        LoginUser loginUser = new LoginUser();
        loginUser.setUserName(loginBody.getUserName());
        loginUser.setUuid(uuid);
        loginUser.setUserInfo(userInfo);
        String token = jwtTokenUtil.createToken(loginUser);
        return Result.success(token);
    }

    @GetMapping("/getUserInfo")
    public Result getUserInfo(){
        return Result.success(UserInfoContext.getUserInfo());
    }

    @PostMapping("/updateUserInfo")
    public Result updateUserInfo(@RequestBody UserInfo userInfo, HttpServletRequest request){
        LoginUser loginUser = jwtTokenUtil.getLoginUser(request);
        userInfoMapper.updateById(userInfo);
        loginUser.setUserInfo(userInfo);
        jwtTokenUtil.refreshToken(loginUser);
        return Result.success(userInfo);
    }

    @PostMapping("/register")
    public Result register(@RequestBody UserInfo userInfo){
        QueryWrapper<UserInfo> userInfoQueryWrapper = new QueryWrapper<>();
        userInfoQueryWrapper.eq("user_name",userInfo.getUserName());
        UserInfo dbData = userInfoMapper.selectOne(userInfoQueryWrapper);
        if (dbData!=null) {
            return Result.fail("当前用户已经存在");
        }
        userInfo.setAvatar("https://img2.baidu.com/it/u=1723711398,3271493933&fm=253&app=138&f=JPEG?w=923&h=800");
        userInfo.setId(UUID.randomUUID().toString().replace("-",""));
        userInfo.setJoinTime(LocalDateTime.now());
        userInfoMapper.insert(userInfo);
        return Result.success();
    }

}
